package com.jt.www.biz.user_wxb.service.impl;

import com.jt.www.biz.agree.service.AgreePrivacyLogService;
import com.jt.www.biz.user_wxb.service.LoginService;
import com.jt.www.domain.enums.wxb.LoginTypeEnum;
import com.jt.www.domain.enums.wxb.ProtocolTypeEnum;
import com.jt.www.domain.vo.LoginVO;
import com.jt.www.domain.vo.RegisterAndLoginVO;
import com.jt.www.enums.ResultEnum;
import com.jt.www.model.remote.user.LoginResultVO;
import com.jt.www.model.reps.GenericDataResponse;
import com.jt.www.model.reps.ResultDTO;
import com.jt.www.remote.UserClient;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 我学保登录接口实现
 * Created by shaoyn on 2020/02/25.
 */
@Service
public class LoginServiceImpl implements LoginService {

    public static final Logger log = LoggerFactory.getLogger(LoginServiceImpl.class);

    @Autowired
    private UserClient userClient;
    @Autowired
    private AgreePrivacyLogService agreePrivacyLogService;

    @Override
    public ResultDTO registerAndLogin(LoginVO loginVO) {
        GenericDataResponse<LoginResultVO> loginResult = null;
        try {
            // 如果是密码登录 调用登录接口
            if (loginVO.getLoginType().equals(LoginTypeEnum.PASSWORD.getCode())) {
                loginResult = userClient.login(loginVO);
            } else {
                //如果是验证码登录 调用注册与登录接口
                loginResult = userClient.registerAndLogin(new RegisterAndLoginVO(loginVO.getPhoneNum(), loginVO.getKaptchaCode()));
            }
            if (null == loginResult) {
                return ResultDTO.createDefaultResultFail(ResultEnum.ERROR_9999, "网络异常稍后再试!");
            }
            if (ResultEnum.ERROR_9999.getCode().equals(loginResult.getCode())) {
                return ResultDTO.createDefaultResultFail(ResultEnum.ERROR_9999, loginResult.getMsg());
            }
        } catch (Exception e) {
            log.info("调用用户中心接口出错：" + e.getMessage());
            return ResultDTO.createDefaultResultFail(ResultEnum.ERROR_9999, "网络异常稍后再试!");
        }
        return verifyFirstLogin(loginResult.getData());
    }

    /**
     * 是否埋点接口
     *
     * @param data
     * @return
     */
    private ResultDTO verifyFirstLogin(LoginResultVO data) {
        String userCode = data.getUserCode();
        if (null == data || StringUtils.isBlank(userCode)) {
            return ResultDTO.createDefaultResultFail(ResultEnum.ERROR_9999, "登录异常，请重试!");
        }

        ResultDTO resultDTO;

        if (agreePrivacyLogService.searchAgreePrivacyLogCount(data.getUserCode(),
                ProtocolTypeEnum.WXB_PRIVACY.getChannelCode(), ProtocolTypeEnum.WXB_PRIVACY.getType()) == 0) {
            //返回埋点标识 及用户信息
            resultDTO = ResultDTO.createDefaultResultFail(ResultEnum.REDIRECT);
        } else {
            resultDTO = ResultDTO.createDefaultResultFail(ResultEnum.SUCCESS_0000);
        }
        //返回用户信息
        resultDTO.setData(data);
        return resultDTO;
    }


    /**
     * 是否第一次登录
     *
     * @param userCode
     * @return
     */
    public ResultDTO isFirstLogin(String userCode) {
        ResultDTO resultDTO = ResultDTO.createDefaultResultFail(ResultEnum.SUCCESS_0000);
        if (agreePrivacyLogService.searchAgreePrivacyLogCount(userCode,
                ProtocolTypeEnum.WXB_PRIVACY.getChannelCode(), ProtocolTypeEnum.WXB_PRIVACY.getType()) == 0) {
            //返回埋点标识 及用户信息
            resultDTO.setData(true);
        } else {
            resultDTO.setData(false);
        }
        return resultDTO;
    }

}
